package com.business_clean.app.uitl;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.business_clean.app.ext.LoadingDialogExtKt;
import com.business_clean.app.uitl.share.ShareHelperTools;
import com.business_clean.app.uitl.share.ShareParams;
import com.business_clean.app.uitl.share.ShareType;

import java.io.File;

/**
 * 统一封装的下载，用这个，统一处理文件处理
 */
public class DownLoadHelper {

    public static void downloadFile(Context context, String downloadUrl, String dialogText, String fileName, boolean shareToWeChat) {
        downloadFile(context, downloadUrl, dialogText, fileName, shareToWeChat, null);
    }


    public static void downloadFile(Context context, String downloadUrl, String dialogText, String fileName, boolean shareToWeChat, DownloadUtil.DownloadListener listener) {
        if (context == null || TextUtils.isEmpty(downloadUrl)) {
            return;
        }
        if (TextUtils.isEmpty(dialogText)) {
            LoadingDialogExtKt.showLoadingExt((AppCompatActivity) context, "下载文件中...");
        } else {
            LoadingDialogExtKt.showLoadingExt((AppCompatActivity) context, dialogText);
        }
        //检查本地是否有提前下载好的文件，如果有 就 true 否认false userLocalFile
        String suffix = "";
        if (downloadUrl.contains(".")) {
            String fileExtension = downloadUrl.substring(downloadUrl.lastIndexOf("."));//获取文件名的后缀
            suffix = fileExtension;
        } else {
            suffix = "";//如果没有就默认""
        }
        String finalSuffix = suffix;
        DownloadUtil.downloadFile(downloadUrl, suffix, fileName, true, new DownloadUtil.DownloadListener() {
            @Override
            public void onDownloadProgress(int progress) {
                LogUtils.e("下载文件中-->" + progress);
            }

            @Override
            public void onDownloadSuccess(File file) {
                LogUtils.e("文件下载成功-->" + file.getAbsolutePath());
                if (listener != null) {
                    listener.onDownloadSuccess(file);
                }
                LoadingDialogExtKt.dismissLoadingExt((AppCompatActivity) context);
                if (shareToWeChat) {
                    shareToWeChat(context, fileName + finalSuffix, file.getAbsolutePath());
                }
            }

            @Override
            public void onDownloadFailed(Exception e) {
                ToastUtil.show("文件下载失败，请稍后再试");
                LoadingDialogExtKt.dismissLoadingExt((AppCompatActivity) context);
            }
        });
    }

    private static void shareToWeChat(Context context, String fileName, String filePath) {
        LogUtils.e("分享的文件名-->" + fileName);
//        ShareParams params = new ShareParams();
//        params.setShareType(ShareType.WEIXIN);
//        params.setLinkUrl(ShareHelperTools.getInstance().adapterUri(filePath));
//        params.setTitle(fileName);
//        ShareHelperTools.getInstance().shareFileToWx(params, (Activity) context);
        ShareUtils.shareWechatFriend(context, new File(filePath));
    }
}
